package com.eight.lesson5.trade;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class TradeTest {

	public static void main(String[] args) {
		Trader raoul = new Trader("Raoul", "Cambridge");
		Trader mario = new Trader("Mario", "Milan");
		Trader alan = new Trader("Alan", "Cambridge");
		Trader brian = new Trader("Brian", "Cambridge");
		
		List<Transaction> transactions = Arrays.asList(
				new Transaction(brian, 2011, 300),
				new Transaction(raoul, 2012, 1000),
				new Transaction(raoul, 2011, 400),
				new Transaction(mario, 2012, 710),
				new Transaction(mario, 2012, 700),
				new Transaction(alan, 2012, 950));
		
		System.out.println("找出2011年发生的所有交易，并按交易额排序（从低到高）。");
		System.out.println("1:");
		transactions.stream()
		.filter(t -> t.getYear()==2011)
		.sorted((t1,t2) -> t1.getValue()-t2.getValue())
		.forEach(System.out::println);
		System.out.println("2:");
		transactions.stream()
		.filter(t -> t.getYear()==2011)
		.sorted(Comparator.comparing(Transaction::getValue))
		.forEach(System.out::println);
		
		System.out.println("交易员都在哪些不同的城市工作过？");
		List<String> citys = transactions.stream()
				.map(Transaction::getTrader)
				.map(Trader::getCity)
				.distinct()
				.collect(Collectors.toList());
		citys.stream().forEach(System.out::println);
		
		System.out.println("查找所有来自于剑桥的交易员，并按姓名排序");
		System.out.println("1:");
		List<Trader> traders = transactions.stream()
				.map(Transaction::getTrader)
				.filter(t -> "Cambridge".equals(t.getCity()))
				.sorted((t1,t2) -> t1.getName().compareTo(t2.getName()))
				.collect(Collectors.toList());
		traders.stream().forEach(System.out::println);
		System.out.println("2:");
		List<Trader> traders2 = transactions.stream()
				.map(Transaction::getTrader)
				.filter(t -> "Cambridge".equals(t.getCity()))
				.distinct()
				.sorted(Comparator.comparing(Trader::getName))
				.collect(Collectors.toList());
		traders2.stream().forEach(System.out::println);
		
		System.out.println("返回所有交易员的姓名字符串，按字母顺序排序");
		System.out.println("1:");
		List<String> names = transactions.stream()
				.map(Transaction::getTrader)
				.map(Trader::getName)
				.distinct()
				.sorted((n1,n2) -> n1.compareTo(n2))
				.collect(Collectors.toList());
		names.stream().forEach(System.out::println);
		System.out.println("2:");
		String nameString = transactions.stream()
				.map(t -> t.getTrader().getName())
				.distinct()
				.sorted()
				.reduce("", (n1,n2) -> n1+n2);
		System.out.println("姓名字符串："+nameString);
		
		System.out.println("有没有交易员是在米兰工作的？");
		boolean isMilan = transactions.stream()
		.map(Transaction::getTrader)
		.anyMatch(t -> "Milan".equals(t.getCity()));
		System.out.println(isMilan?"有":"无");
		
		System.out.println("打印生活在剑桥的交易员的所有交易额");
		List<Integer> values = transactions.stream()
				.filter(t -> "Cambridge".equals(t.getTrader().getCity()))
				.map(Transaction::getValue)
				.collect(Collectors.toList());
		values.stream().forEach(System.out::println);
		
		System.out.println("所有交易中，最高的交易额是多少");
		Optional<Integer> valueMax = transactions.stream()
				.map(Transaction::getValue)
				.reduce(Integer::max);
		System.out.println(valueMax.get());
		
		System.out.println("找到交易额最小的交易");
		Optional<Transaction> minTransaction = transactions.stream()
				.reduce((t1,t2) -> t1.getValue()<t2.getValue()?t1:t2);
		System.out.println(minTransaction.get());
		Optional<Transaction> minTransaction2 = transactions.stream()
				.min(Comparator.comparing(Transaction::getValue));
		System.out.println(minTransaction2.get());
	}

}
